package com.pqtest.api.controller;

import com.alibaba.fastjson.JSONObject;
import com.pqtest.api.annotation.ResponseFormat;
import com.pqtest.api.dto.CollectionDto;
import com.pqtest.api.entity.CollectionEntity;
import com.pqtest.api.entity.PqCsTestItemEntity;
import com.pqtest.api.service.CollectionService;
import com.pqtest.api.service.UserService;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.*;

@RestController
@Log
public class CollectionController extends BaseController {
    @Autowired
    UserService userService;
    @Autowired
    CollectionService collectionService;
    @GetMapping("/collect")
    @ResponseFormat(type = "withTotal")
    Object myCollection(OAuth2Authentication oAuth2Authentication){
        //String uname = (String)oAuth2Authentication.getUserAuthentication().getPrincipal();
        //stringRedisTemplate.opsForValue().set("lake","haha");
        String uid = this.getUid(oAuth2Authentication);
        List<CollectionEntity> list =  userService.getColList(Integer.parseInt(uid));
        List<PqCsTestItemEntity> finals = new ArrayList<PqCsTestItemEntity>();
        for(CollectionEntity c : list){
           finals.add(c.getItem());
        }
        return finals;
        //return userService.getList();
    }
    @DeleteMapping("/collect")
    @ResponseFormat(type = "withMsg")
    Object delCollect(@Valid CollectionDto collectionDto,BindingResult bindingResult,OAuth2Authentication oAuth2Authentication) throws Exception {
        if(bindingResult.hasErrors()){
            throw new Exception(bindingResult.getFieldError().getDefaultMessage());
        }
        String uid = this.getUid(oAuth2Authentication);
        collectionDto.setUserId(Integer.parseInt(uid));
        collectionService.deCollect(collectionDto);
        return "Success";
    }

    @PostMapping("/collect")
    @ResponseFormat(type = "withMsg")
    Object collect(@Valid CollectionDto collectionDto, BindingResult bindingResult, OAuth2Authentication oAuth2Authentication) throws Exception {
        if(bindingResult.hasErrors()){
            throw new Exception(bindingResult.getFieldError().getDefaultMessage());
        }
        String uid = this.getUid(oAuth2Authentication);
        collectionDto.setUserId(Integer.parseInt(uid));
        collectionService.collect(collectionDto);
        return "Success";
    }

    //错题本
    @GetMapping("/wrong")
    @ResponseFormat(type = "withTotal")
    Object myWrong(OAuth2Authentication oAuth2Authentication){
        //String uname = (String)oAuth2Authentication.getUserAuthentication().getPrincipal();
        //stringRedisTemplate.opsForValue().set("lake","haha");
        String uid = this.getUid(oAuth2Authentication);
        List<CollectionEntity> list =  userService.getWrongList(Integer.parseInt(uid));
        List<PqCsTestItemEntity> finals = new ArrayList<PqCsTestItemEntity>();
        for(CollectionEntity c : list){
            PqCsTestItemEntity pt = c.getItem();
            pt.setIs_collected("0");
            pt.setAnswerIds(c.getAnswerIds());
            finals.add(pt);
        }
        return finals;
        //return userService.getList();
    }
    @PostMapping("/wrong")
    @ResponseFormat(type = "withMsg")
    Object wrong(@Valid CollectionDto collectionDto, BindingResult bindingResult, OAuth2Authentication oAuth2Authentication) throws Exception {
        if(bindingResult.hasErrors()){
            throw new Exception(bindingResult.getFieldError().getDefaultMessage());
        }
        String uid = this.getUid(oAuth2Authentication);
        collectionDto.setUserId(Integer.parseInt(uid));
        collectionService.wrong(collectionDto);
        return "Success";
    }
    @DeleteMapping("/wrong")
    @ResponseFormat(type = "withMsg")
    Object delWrong(@Valid CollectionDto collectionDto,BindingResult bindingResult,OAuth2Authentication oAuth2Authentication) throws Exception {
        if(bindingResult.hasErrors()){
            throw new Exception(bindingResult.getFieldError().getDefaultMessage());
        }
        String uid = this.getUid(oAuth2Authentication);
        collectionDto.setUserId(Integer.parseInt(uid));
        collectionService.deWrong(collectionDto);
        return "Success";
    }
}